package com.aaa.hospital.ManagementEnd.controller;


import com.aaa.hospital.ManagementEnd.pojo.DrugGathering;
import com.aaa.hospital.ManagementEnd.pojo.ExpenseCalendar;
import com.aaa.hospital.ManagementEnd.pojo.Prescription;
import com.aaa.hospital.ManagementEnd.service.ExpenCalendService;
import com.aaa.hospital.ManagementEnd.service.Impl.MedicinImpl;
import com.aaa.hospital.ManagementEnd.service.MedChargeSerevice;
import com.aaa.hospital.ManagementEnd.service.PrescriptionService;
import com.aaa.hospital.ManagementEnd.util.MecharDB;
import com.aaa.hospital.ManagementEnd.util.MedicineCharge;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

//lgq
//主掌握药品收费页面
@Controller
@RequestMapping("/Medcharge")
public class MedChargeConreoller {

    //药单
    @Autowired
    MedChargeSerevice medChargeSerevice;
    @Autowired
    private PrescriptionService prescriptionService;
    @Autowired
    private MedicinImpl medicinImpl;
    @Autowired
    private ExpenCalendService expenCalendService;
    @RequestMapping("/MedChargeAll")
    @ResponseBody
   public List<MedicineCharge>  MedChargeAll(Integer diaid){
//        JSONObject json = new JSONObject();
        //通过药单编号，查询具体信息
       List<MedicineCharge> medicineCharges = medChargeSerevice.MedChargeAll(diaid);
       return medicineCharges;
   }


    @RequestMapping("/dginsert")
    @ResponseBody
    public boolean dginsert(Integer visid, Integer diaid, Integer price,String usefor, HttpSession session){
        String user =(String) session.getAttribute("user");
        Integer did=new Integer(user);
        DrugGathering dgther=new DrugGathering();
        dgther.setVisid(visid);
        dgther.setNurseid(did);
        dgther.setDiaid(diaid);
        dgther.setPrice(price);
        boolean dginsert = medChargeSerevice.dginsert(dgther); //添加取药记录单
        if (dginsert == true) {
            boolean byvisid = medChargeSerevice.Byvisid(visid, price);//判断月是否足够支付，够，直接减去金额，反之返回false
            ExpenseCalendar ex=new ExpenseCalendar();
            ex.setVisid(visid);
            ex.setPurpose(usefor);
            ex.setMoney(price);
            if (byvisid == true) {
                expenCalendService.ecInsert(ex);// 添加消费记录信息
                List<Prescription> prescriptions = prescriptionService.selectAll(diaid);//查询药单
                boolean upunm=true;
                for (Prescription o: prescriptions) {
                    upunm = medicinImpl.upnum(o.getYpid(), o.getYpnum());//对药品数量进行减除
                }
                medChargeSerevice.ydstate(diaid);
                return upunm;

            }else {
                return false;
            }

        }else {
            return false;
        }
    }



 @RequestMapping(value = "/queryList",method = RequestMethod.GET)
 @ResponseBody
 public String queryList(){
     JSONObject json = new JSONObject();
     List<MecharDB> mecharDBS = medChargeSerevice.queryList();
     Object objData=JSONObject.toJSON(mecharDBS);
     json.put("code", 0);
     json.put("msg", "");
     json.put("count",mecharDBS.size());
     json.put("data",objData);
     return json.toJSONString();
 }
    @RequestMapping("/queryListById")
    @ResponseBody
    public List<MedicineCharge>  queryListById(Integer diaid){
//        JSONObject json = new JSONObject();
        List<MedicineCharge> medicineCharges = medChargeSerevice.queryListById(diaid);
        return medicineCharges;
    }

    @RequestMapping("/queryListlimit")
    @ResponseBody
    public String  queryListlimit(String dname){
        JSONObject json = new JSONObject();
        List<MecharDB> medicineCharges = medChargeSerevice.queryListlimit(dname);
        Object objData=JSONObject.toJSON(medicineCharges);
        json.put("code", 0);
        json.put("msg", "");
        json.put("count",medicineCharges.size());
        json.put("data",objData);
        return json.toJSONString();
    }



}
